import { useHistory, useModel } from 'umi';
import ShowProgramDetail from '@/components/ShowProgramDetail';
import { ModelReturn, ProgramDetail } from '@/data-type';
import { getIdFromHistory, needbackButton } from '@/utils/kits';
import { memo, useEffect, useState } from 'react';
const ProgramPageDetail: React.FC = () => {
  // const { dataSource, reload, deleteById, updateOne } = useModel('ProgramDetailModel');
  const model = useModel('ProgramDetailModel');
  const reload = () => { return model.query({}) };
  const history = useHistory();
  const [dataSource, setDataSource] = useState<ProgramDetail[]>([]);
  // debugger
  useEffect(() => {
    if ((!dataSource || dataSource.length === 0) && !needbackButton(history.location.state)) {
      model.query({}).then(data => {
        setDataSource(data as ProgramDetail[]);
      })
    } else {
      let id: number = getIdFromHistory(history.location.state) ?? 0;
      model.query({ programId: id })
        .then(data => {
          setDataSource((data as ProgramDetail[]));
      })
    }
  },[])
  console.log("运行之后", dataSource)
  // debugger
  const retModel: ModelReturn<ProgramDetail> = {
    ...model,
    dataSource: dataSource,
    reload: reload,
  }
  return (
    <div>
      <a onClick={() => retModel.reload()}>reload</a>
      <ShowProgramDetail model={retModel} />
    </div>
  );
};

export default ProgramPageDetail;